package com.example.zhou.connect;

import com.example.zhou.callback.LatestNewsCallback;
import com.example.zhou.url.ZhihuUrl;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 11065 on 2016/10/27.
 */

public class LastestNewsConnect implements LastestNewsInterface {

    @Override
    public void getLatestNews(final LatestNewsCallback latestNewsCallback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(ZhihuUrl.NEWSLATEST)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                latestNewsCallback.onLastestNeswFail(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                latestNewsCallback.onLastestNewsSuccess(response.body().string());
            }


        });
    }
}

interface LastestNewsInterface {
    void getLatestNews(LatestNewsCallback latestNewsCallback);
}
